﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace FileDuplicateUtility
{
    /// <summary>
    /// Interaction logic for FilterList.xaml
    /// </summary>
    public partial class FilterList : UserControl
    {
        public FilterList()
        {
            Filters = new ObservableCollection<Filter>();

            InitializeComponent();
        }

        public ObservableCollection<Filter> Filters { get; private set; }

        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set { SetValue(SelectedIndexProperty, value); }
        }

        public static readonly DependencyProperty SelectedIndexProperty =
            DependencyProperty.Register("SelectedIndex", typeof(int), typeof(FilterList), new UIPropertyMetadata(0));

        public enum FilterMoveAction
        {
            Up = 0,
            Add = 1,
            Remove = 2,
            Down = 3
        }

        private void MoveFilterCmd_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            FilterMoveAction action;
            if (Enum.TryParse<FilterMoveAction>(e.Parameter.ToString(), out action))
            {
                var index = SelectedIndex;
                switch (action)
                {
                    case FilterMoveAction.Up:
                        if (index > 0)
                        {
                            var filter = Filters[index];
                            Filters.RemoveAt(index);
                            Filters.Insert(index - 1, filter);
                            SelectedIndex = index - 1;
                        }
                        break;
                    case FilterMoveAction.Down:
                        if (index < Filters.Count - 1)
                        {
                            var filter = Filters[index];
                            Filters.RemoveAt(index);
                            Filters.Insert(index + 1, filter);
                            SelectedIndex = index + 1;
                        }
                        break;
                    case FilterMoveAction.Add:
                        {
                            var filter = Selector.SelectedFilter;
                            if (filter.AllowMultiple == false && Filters.Any(x => x.GetType() == filter.GetType()))
                            {
                                break;
                            }
                            Filters.Add(filter.Copy());
                            filter.Reset();
                            SelectedIndex = Filters.Count - 1;
                        }
                        break;
                    case FilterMoveAction.Remove:
                        if (index >= 0)
                        {
                            Filters.RemoveAt(index);
                            if (Filters.Count > 0)
                            {
                                if (index == Filters.Count)
                                {
                                    SelectedIndex = index - 1;
                                }
                                else
                                {
                                    SelectedIndex = index;
                                }
                            }
                        }
                        break;
                }
            }
        }
    }
}
